package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.TaskCompleteContract109;
import com.lkd.emq.Topic;
import com.lkd.http.view.TokenObject;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Objects;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-12-02 11:27
 */
@Component
@Topic(TopicConfig.VMS_COMPLETED_TOPIC)
@Slf4j
public class TaskCompleteHandler implements MsgHandler {

    @Autowired
    private VendingMachineService vendingMachineService;

    @Override
    public void process(String jsonMsg) throws IOException {
        System.out.println(jsonMsg);
        if(!StringUtils.isEmpty(jsonMsg)){
            TaskCompleteContract109 contract =
                    JsonUtil.getByJson(jsonMsg, TaskCompleteContract109.class);
            if(StringUtils.isEmpty(contract.getInnerCode()) || contract.getType() == null){
                log.error("消息协议数据为空" + jsonMsg);
                return;
            }

            //如果是投放工单，将售货机状态修改为运营
            if(Objects.equals(contract.getType(),TaskCompleteContract109.TaskCompleteContractType.DEPLOY)){
                vendingMachineService.updateStatus(contract.getInnerCode(), VMSystem.VM_STATUS_RUNNING);
            }

            //如果是撤机工单，将售货机状态修改为撤机
            else if(Objects.equals(contract.getType(),TaskCompleteContract109.TaskCompleteContractType.REVOKE)){
                vendingMachineService.updateStatus(contract.getInnerCode(), VMSystem.VM_STATUS_REVOKE);
            }
        }

    }
}
